package com.webserver.controller;

import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.webserver.vo.User;

import java.io.*;

/**
 * 处理与用户相关的请求操作
 */
public class UserController {
    private static File userDir;

    static {
        userDir = new File("./users");
        if (!userDir.exists()) {
            userDir.mkdirs();
        }
    }

    /**
     * 处理用户注册操作的请求
     */
    public void reg(HttpServletRequest request, HttpServletResponse response) {

        System.out.println("开始处理注册....");
        //1.获取用户在注册页面上表单中提交上来的注册信息
        //调用getParameter用于获取浏览器传递过来的参数.
        //参数与表单中输入框的名字一致
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        System.out.println(username + "," + password + "," + nickname + "," + ageStr);
        /*
            必要的验证工作：
            1：要求在上述四个信息用户必须在页面上输入，不能有null的
            2：年龄还必须是一个数字格式
            否则直接响应一个页面：reg_info_error.html
            该页面居中显示一行字：注册失败,注册信息输入有误,请重新注册.
         */
        if (username==null|| password==null||nickname==null||
                ageStr == null||!ageStr.matches("[0-9]+")) {
            File file = new File("./webapps/myweb/reg_info_error.html");
            response.setEntity(file);
            return;
        }
        /*
            用户名不能重复,若是重复用户名则直接响应页面：reg_have_user.html
            页面居中显示一行字：该用户已存在,请重新注册
         */
        File userFile = new File(userDir,username+".obj");
        if (userFile.exists()){
            userFile = new File("./webapps/myweb/reg_have_user.html");
            response.setEntity(userFile);
            return;
        }
            int age = Integer.parseInt(ageStr);
            //2.将该用户信息保存
            //将当前用户以一个User对象形式序列化到文件中：用户名：.obj
            User user = new User(username, password, nickname, age);
            try (
                    ObjectOutputStream oos = new ObjectOutputStream(
                            new FileOutputStream( //改文件或目录不存在会自动创建目录
                                    new File(userDir, username + ".obj") //表示userDir目录下的一个目录
                            )
                    );
            ) {
                oos.writeObject(user);
                System.out.println("写出完毕！");

                File file = new File("./webapps/myweb/reg_success.html");
                response.setEntity(file);

            } catch (IOException e) {
                e.printStackTrace();
            }
            //3.设置响应对象给用户回馈注册结果
            System.out.println("注册处理完毕!");
    }

    /**
     * 处理用户登录
     * @param request
     * @param response
     */
    public void login(HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理登录.....");
        //用户登录时输入的用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username+","+password);

        if (username==null || password==null){
            File file = new File("./webapps/myweb/login_error.html");
            response.setEntity(file);
            return;
        }

        //获取与用户输入用户名对应的文件
        File fileName = new File(userDir,username+".obj");
        if (fileName.exists()){
            try ( //反序列化对应的user对象
                  ObjectInputStream ois = new ObjectInputStream(
                          new FileInputStream(fileName)
                    )
                  ){
                User user = (User) ois.readObject();
                //比较密码
                if ( password.equals(user.getPassword())){
                    //密码一致，登录成功
                   File file = new File("./webapps/myweb/login_success.html");
                   response.setEntity(file);
                   return;
               }
            }catch (IOException | ClassNotFoundException e){
                e.printStackTrace();
            }
        }
        //处理登录失败
        File file = new File("./webapps/myweb/login_fail.html");
        response.setEntity(file);

        System.out.println("登录处理完毕!");

    }

}
